﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using CVsharp.Models.Entities;

namespace CVsharp.MVClib.Authorize
{
    public class ActionPermissionService
    {
        string NamePaceStr;

        #region IActionPermissionService 成员
        /// <summary>
        /// 获取所以mvc的action信息
        /// </summary>
        /// <returns></returns>
        public IList<sysActionPermission> GetAllActionByAssembly(string namePaceStr)
        {
            string[] Result = new string[] { 
                "ActionResult", 
                "ViewResult", 
                "EmptyResult", 
                "RedirectResult", 
                "JsonResult",
                "JavaScriptResult",
                "ContentResult",
                "FileContentResult" };
            NamePaceStr = namePaceStr;
            var result = new List<sysActionPermission>();
            var types = Assembly.Load(namePaceStr).GetTypes();
            foreach (var type in types)
            {
                if (type.BaseType.Name != "BaseController") continue;
                var members = type.GetMethods();
                foreach (var member in members)
                {
                    if (!Result.Contains(member.ReturnType.Name)) continue;
                    object[] attrs = member.GetCustomAttributes(typeof(IsViewPageAttribute), true);
                    bool isViewPage = attrs.Length == 1;//当前Action请求是否为具体的功能页
                    var ap = new sysActionPermission
                    {
                        ActionName = member.Name,
                        ControllerName =
                            member.DeclaringType.Name.Substring(0, member.DeclaringType.Name.Length - 10),
                        IsViewPage = isViewPage
                    };
                    attrs = member.GetCustomAttributes(typeof(DescriptionAttribute), true);//获取注释
                    ap.Description = attrs.Length > 0 ? ((DescriptionAttribute)attrs[0]).Description : "";
                    result.Add(ap);
                }
            }
            return result;
        }
        /// <summary>
        /// 根据分页获取action列表
        /// </summary>
        /// <param name="query"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public IList<sysActionPermission> QueryActionPlist(string query, int start, int limit, out long total)
        {
            IList<sysActionPermission> allActions = GetAllActionByAssembly(NamePaceStr);
            total = (
                from a in allActions
                where a.ActionName.ToLower().Contains(query.ToLower())
                select a
                ).Count();

            var result = (
                from a in allActions
                where a.ActionName.ToLower().Contains(query.ToLower())
                select a
                ).Skip(start).Take(limit);
            return new List<sysActionPermission>(result);
        }
        public bool Exit(sysActionPermission sap)
        {
            IList<sysActionPermission> allActions = GetAllActionByAssembly(NamePaceStr);
            return allActions.Count(m => m.ActionName == sap.ActionName &&
                m.ControllerName == sap.ControllerName)
                > 0;
        }

        #endregion
    }
}
